package com.jg.test;

import com.jg.test.Page.Page;
import com.rw.tool.util.io.FileIOUtil;
import com.rw.tool.util.random.RandomUtil;
import com.rw.tool.util.string.StringFormatter;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
public class Dice {
    // 骰子点数
    private int point;

    // 指示该骰子是否已使用
    private boolean isUsed = false;

    // 指示该骰子是否已经重掷
    private boolean isReRoll = false;

    // 初始化骰子
    public static List<Dice> dice = new ArrayList<>();

    // 重掷次数
    public static int reRollChance = 3;

    public static void setDice() {
        for (int i = 0; i < 6; i++) {
            Dice dice = new Dice();
            Dice.dice.add(dice);
        }
    }

    /**
     * 生成6枚随机点数的骰子列表
     */
    public static void roll() {
        for (Dice dice : Dice.dice) {
            dice.setPoint(RandomUtil.nextInt(6) + 1);
        }
    }

    public static void diceShow() {
        System.out.println("当前投掷结果为：");
        for (Dice dices : dice) {
            System.out.print(dices.getPoint() + "  ");
        }
    }

    /**
     * 重掷骰子，有次数限制
     */
    public static void reRoll() {
        // 可重掷次数
        int reRollChance = Dice.reRollChance;

        System.out.println();
        Dice.diceShow();
//        System.out.println();
//        System.out.println("输入骰子序号将该骰子重掷。");
//        System.out.println();
//        System.out.println("点数后加”*“号意味着该骰子已经重掷，再次选择将开始新一轮重掷");
//        System.out.println("当前可重掷次数：" + reRollChance + "次，输入6结束选择");

        // 读取配置文件
        String rawInformation = FileIOUtil.readFileFromClassPath("DiceInformation.txt");

        // 填充参数
        String information = StringFormatter.format(rawInformation, reRollChance);

        // 输出
        System.out.println(information);


        // TODO 缺注释
        while (reRollChance > 0) {
            int choice = Page.numGet(6);

            if (choice == 6) {
                break;
            }

            if (dice.get(choice).isReRoll()) {
                for (Dice dice : dice) {
                    dice.setReRoll(false);
                }
                reRollChance--;
                if (reRollChance == 0) {
                    break;
                }
                String template = "剩余重掷次数：{}次。";
                String format = StringFormatter.format(template, (reRollChance - 1));
                System.out.println(format);
            }

            while (choice != 6) {
                dice.get(choice).setReRoll(true);
                dice.get(choice).setPoint(RandomUtil.nextInt(6) + 1);
                for (Dice dice : dice) {
                    System.out.print(dice.getPoint());
                    if (dice.isReRoll()) {
                        System.out.print("*  ");
                    } else {
                        System.out.print("  ");
                    }
                }
                choice = 6;
            }
        }

        System.out.println("重掷结束。");
    }
}
